package com.xnyzc.lhy.order.service.impl.vehicle.collect;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.constant.MessageConstant;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.order.entity.result.vehicle.VehicleCollectionRes;
import com.xnyzc.lhy.order.entity.vehicle.collect.OaCVehicleCollect;
import com.xnyzc.lhy.order.entity.vehicle.OaSysVehicle;
import com.xnyzc.lhy.order.mapper.vehicle.collect.OaCVehicleCollectMapper;
import com.xnyzc.lhy.order.service.vehicle.collect.IOaCVehicleCollectService;
import com.xnyzc.lhy.order.service.system.IOaSysVehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户车辆收藏 服务实现类
 * </p>
 *
 * @author bin
 * @since 2019-07-18
 */
@Service
public class OaCVehicleCollectServiceImpl extends ServiceImpl<OaCVehicleCollectMapper, OaCVehicleCollect> implements IOaCVehicleCollectService {

    @Autowired
    private OaCVehicleCollectMapper oaCVehcileCollectMapper;
    @Autowired
    private IOaSysVehicleService iOaSysVehicleService;

    /**
     * 查询全部用户车辆收藏
     * @param userId 乘客端用户id
     * @param current 当前页
     * @return
     */
    @Override
    public IPage<VehicleCollectionRes> selectVehicleCollectPage(Long userId, long current, String vehNo) {
        IPage<VehicleCollectionRes> iPage = new Page<>();
        iPage.setCurrent(current);
        IPage<VehicleCollectionRes> page = oaCVehcileCollectMapper.selectVehicleCollectionPage(iPage, 0, userId, 1, vehNo);
        return page;
    }

    /**
     * 收藏车辆
     * @param userId 用户id
     * @param vehicleId 车辆id
     * @return
     */
    @Override
    public Map<String, Object> collectVehicle(Long userId, Long vehicleId){
        Map<String, Object> result = new HashMap<>(2);
        result.put("code", -1);
        // 判断是否存在这辆汽车
        OaSysVehicle oaSysVehicle = iOaSysVehicleService.getVehicle(vehicleId);
        if (oaSysVehicle == null){
            result.put("msg", MessageConstant.COLLECT_VEHICLE_UNKNOWN);
            return result;
        }
        // 判断是否收藏过
        OaCVehicleCollect vehicleCollection = this.getVehicleCollection(userId, vehicleId);
        if (vehicleCollection != null){
            result.put("msg", MessageConstant.COLLECT_VEHICLE_REPEAT);
            return result;
        }
        // 收藏
        int i = this.insertCollectVehicle(userId, vehicleId);
        if (i == 1){
            result.put("msg", MessageConstant.COLLECT_VEHICLE_SUCCESS);
            result.put("code", 0);
        } else {
            result.put("msg", MessageConstant.FAIL);
        }
        return result;
    }

    /**
     * 插入车辆信息
     * @param userId 用户id
     * @param vehicleId 车辆id
     * @return
     */
    private int insertCollectVehicle(Long userId, Long vehicleId) {
        Date date = new Date();
        OaCVehicleCollect oaCVehcileCollect = new OaCVehicleCollect();
        oaCVehcileCollect.setCollectId(IDUtil.nextId());
        oaCVehcileCollect.setUserId(userId);
        oaCVehcileCollect.setVehOrDriverId(vehicleId);
        // 0：司机、1：车辆
        oaCVehcileCollect.setCollectType(1);
        oaCVehcileCollect.setCreateUser(userId);
        oaCVehcileCollect.setCreateTime(date);
        oaCVehcileCollect.setUpdateUser(userId);
        oaCVehcileCollect.setUpdateTime(date);
        return oaCVehcileCollectMapper.insert(oaCVehcileCollect);
    }

    /**
     * 根据用户id与车辆id查询收藏车辆
     * @param userId 用户id
     * @param vehicleId 车辆id
     * @return
     */
    public OaCVehicleCollect getVehicleCollection(Long userId, Long vehicleId) {
        return oaCVehcileCollectMapper.selectOne(
                Wrappers.<OaCVehicleCollect>lambdaQuery()
                        .eq(OaCVehicleCollect::getUserId, userId)
                        .eq(OaCVehicleCollect::getVehOrDriverId, vehicleId)
                        .eq(OaCVehicleCollect::getDeleted, "0")
        );
    }
}
